import { Injectable } from '@nestjs/common';
import { Transporter, createTransport } from 'nodemailer';
import { GLOBAL_CONSTANTS } from 'src/global/constants';

@Injectable()
export class EmailService {
  private transporter: Transporter;

  constructor() {
    this.transporter = createTransport({
      host: 'smtp.qq.com',
      port: 465,
      secure: true,
      auth: {
        user: GLOBAL_CONSTANTS.SMTP_QQ,
        pass: GLOBAL_CONSTANTS.SMTP_CODE,
      },
    });
  }

  /**
   * 发送邮件
   */
  async sendEmail(to: string, subject: string, text: string): Promise<void> {
    await this.transporter.sendMail({
      from: 'Orange Personal Server <' + GLOBAL_CONSTANTS.SMTP_QQ + '>',
      to,
      subject,
      text,
    });
  }
}
